import WxValidate from "../../../utils/WxValidate.js";
const app = getApp();
var url = app.getURL();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:'0',
    secret:false,
    picture:[],
    imgList: [], 
    multiArray: [
      ['北区', '中区', '南区', '公寓'],
      ['1栋', '2栋', '3栋', '4栋', '5栋']
    ],
    multiIndex: [0, 0],

    area:'',
    build:'',
    address:'北区1栋',
    message:{
      // name:'请输入您的真实名字',
      // number:'请输入您的学号/工号',
      // major:'请输入您的专业'
    }
  },
  //宿舍多选
  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    
        console.log(this.data.multiArray[0][e.detail.value[0]])
        console.log(this.data.multiArray[1][e.detail.value[1]])
    this.setData({
      multiIndex: e.detail.value,
      area:this.data.multiArray[0][e.detail.value[0]],
      build: this.data.multiArray[1][e.detail.value[1]],
    })
    
    this.data.address=this.data.area + this.data.build
  },
  bindMultiPickerColumnChange: function (e) {
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        switch (data.multiIndex[0]) {
          case 0:
          case 1:
          case 2:
            data.multiArray[1] = ['1栋', '2栋', '3栋', '4栋', '5栋'];
            break;
          case 3:
            data.multiArray[1] = ['A栋', 'B栋', 'D栋', 'E栋', 'F栋', 'G栋', 'H栋'];
            break;
        }
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        break;
    }
    this.setData(data);
    

  },


  // 报错
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
    // 验证信息
  initValidate() {
    const rules = {
      name: {
        required: true,
        maxlength: 10
      },
      number: {
        required: true,
      },
      address:{
        required: true
      },
      major:{
        required: true
      }
    }
  
    const message = {
      name: {
        required: '请输入姓名',
        maxlength: '名字不能超过10个字'
      },
      number: {
        required:'请填写学号',
      }, 
      address: {
        required: '请填写宿舍地址',
      }, 
      major: {
        required: '请输入您的专业',
      }
    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  formSubmit: function (e) {
    let that = this;
    var adds = e.detail.value;
    console.log(adds);
    let tempFilePaths = this.data.imgList
  //校验表单
  if (!this.WxValidate.checkForm(adds)) {
    const error = this.WxValidate.errorList[0]
    this.showModal(error)
    return false
  }
  else{
    //表单验证
    // if(this.data.imgList==''){
    //   wx.showModal({
    //     content: '请上传一张图片',
    //     showCancel: false,
    //   })
    //    return false
    // }
    // 上传图片
    wx.uploadFile({
      url: url+'/fileUpload',
      filePath: tempFilePaths[0],
      name: 'file',
      formData: {
        'folder': 'userImg' 
      },
      success (res){
        var fi = JSON.parse(res.data)//转为对象使用
        console.log(fi)
        var backImgUrl=fi.data
        // console.log(backImgUrl)
        // 发起请求
        wx.request({
          url: url+'/user/app/cust/authInfo',
          data: {
            "authImage": backImgUrl,
            "major": adds.major,
            "name": adds.name,
            "number": adds.number,
            address: that.data.address
          },
          method: 'POST',
          header: {// 设置请求的 header
            'content-type': 'application/json',
            token:wx.getStorageSync('cookieToken')
          },
          success: function (res) {
            console.log(res.data),
            
            // 成功提示
                setTimeout(function(){
                  wx.showToast({
                    title: '提交成功'
                  })
                },1000)
                // setTimeout(() => {
                // wx.navigateBack({
                //  delta: 1,  // 返回上一级页面
                // })
                // }, 1500)
          },
          fail: function (res) {
            console.log('错误1' + ':' + res)
          }
        })
      }
    })
  }
},
    

  // 上传照片
  ChooseImage() {
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
    ViewImage(e) {
      wx.previewImage({
        urls: this.data.imgList,
        current: e.currentTarget.dataset.url
      });
    },
    DelImg(e) {
      wx.showModal({
        title: '',
        content: '确定要删除这张照片吗？',
        cancelText: '再看看',
        confirmText: '确认删除',
        success: res => {
          if (res.confirm) {
            this.data.imgList.splice(e.currentTarget.dataset.index, 1);
            this.setData({
              imgList: this.data.imgList
            })
          }
        }
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate();
    let that = this;
    let message = wx.getStorageSync('allMessage')
    // console.log(message)
    if(!message){
      console.log('空的')
      // 缓存无数据，发起请求
      wx.request({
        url: url+'/user/app/cust/getCustomer',
        method:'GET',
        header: {// 设置请求的 header
          'content-type': 'application/json',
          token:wx.getStorageSync('cookieToken')
        },
        success:function(res){
          console.log(res)
          // console.log(res.data.data.authImage)
          that.setData({
            message: res.data.data,
            address: res.data.data.address,
            imgList: that.data.imgList.concat(res.data.data.authImage)
          })
        }
      })

    }else{
        that.setData({
          message: message,
          address: message.address,
          imgList: that.data.imgList.concat(message.authImage)
        })
    }
    
    // 获取状态
    let id = wx.getStorageSync('message').id;
    wx.request({
      url: url+'/user/app/user/userAuth/auth/queryAuthStatus',
      method: "POST",
      header: {
        'content-type': 'application/json',
        token:wx.getStorageSync('cookieToken')
      },
      data:{
        "authRole": '9901',
        "ownerId": id
      },
      success(res) {
        console.log(res.data)
        if(res.data.data==0){
          that.setData({
            secret:false,
            status:0
          })
        }
        if(res.data.data==1){
          that.setData({
            secret:true,
            status:1
          })
        }
        if(res.data.data==2){
          that.setData({
            secret:true,
            status:2
          })
        }
        if(res.data.data==4){
          wx.showModal({
            title: '温馨提示',
            content:'账号认证不通过，请完善资料重新申请'
          })
          this.setData({
            secret:false,
            status:0
          })
        }
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  exit(){
    wx.navigateBack({
      delta:1
    })
  },
  agree(){
    this.setData({
      secret:true
    })
  }
})